package cn.com.gridinfo.sooc.core.expand.callname.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.activity.QuexiTongjiActivity;
import cn.com.gridinfo.sooc.core.expand.callname.activity.TongjiInfoActivity;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;

/**
 * 点名统计界面列表适配器
 *
 * @author yingchunli
 */
public class TongjiAdapter extends UltimateViewAdapter<TongjiAdapter.ViewHolder> {

    public List<ClassInfo> classInfoList;
    Context context;

    public TongjiAdapter(List<ClassInfo> classInfoList, Context context) {
        this.classInfoList = classInfoList;
        this.context = context;
    }

    public void loadmore(List<ClassInfo> list) {
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<ClassInfo> list) {
        classInfoList.clear();
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 清空数据
     */
    public void clear(){
        classInfoList.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_callname_tongji_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= classInfoList.size() : position < classInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            final ClassInfo classInfo = classInfoList.get(position);
            holder.callnameCount.setText(classInfo.getQdcs());

            //设置上课学期的颜色
            holder.classname.setText(classInfo.getKcmc() + "(" + classInfo.getSkxq() + ")");
            String str = classInfo.getKcmc() + "(" + classInfo.getSkxq() + ")";
            //设置字体颜色
            SpannableStringBuilder builder = new SpannableStringBuilder(holder.classname.getText().toString());
            builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.color_tv333)),
                    0, classInfo.getKcmc().length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.color_666)),
                    classInfo.getKcmc().length(), str.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            holder.classname.setText(builder);

            holder.lookinfoBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到统计详情 界面
                    Intent intent = new Intent(context, TongjiInfoActivity.class);
                    intent.putExtra("kcid", classInfo.getKcid());
                    intent.putExtra("kcmc", classInfo.getKcmc());
                    intent.putExtra("skxq", classInfo.getSkxq());
                    intent.putExtra("qdcs", classInfo.getQdcs());
                    context.startActivity(intent);
                    AnimUtil.intentSlidIn((Activity) context);
                }
            });
            holder.qxtjBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到缺席统计 界面
                    Intent intent = new Intent(context, QuexiTongjiActivity.class);
                    intent.putExtra("kcid", classInfo.getKcid());
                    intent.putExtra("kcmc", classInfo.getKcmc());
                    intent.putExtra("skxq", classInfo.getSkxq());
                    intent.putExtra("dmcs", classInfo.getQdcs());
                    context.startActivity(intent);
                    AnimUtil.intentSlidIn((Activity) context);
                }
            });
            //底部分割线的显示
//            if (position == classInfoList.size() - 1) {
//                holder.line.setVisibility(View.VISIBLE);
//            } else {
//                holder.line.setVisibility(View.GONE);
//            }
        }
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getAdapterItemCount() {
        return classInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView classname, callnameCount, lookinfoBtn, qxtjBtn;
        View line;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                classname = (TextView) itemView.findViewById(R.id.className_tongji);
                callnameCount = (TextView) itemView.findViewById(R.id.count_tongji);
                lookinfoBtn = (TextView) itemView.findViewById(R.id.lookinfo_tongji_item);
                qxtjBtn = (TextView) itemView.findViewById(R.id.quexi_tongji_item);
//                line = itemView.findViewById(R.id.line_bottom);//底部分割线
            }
        }
    }
}
